/*
 * 文件名：MenuServiceImpl.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-20
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.rbac.service.impl;


import static com.qbgs.util.ReflectUtil.reflect;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qbgs.base.TreeEntity;
import com.qbgs.rbac.bean.SysMenu;
import com.qbgs.rbac.dao.MenuDao;
import com.qbgs.rbac.dao.RoleDao;
import com.qbgs.rbac.service.MenuService;


@Service(value = "menuService")
public class MenuServiceImpl implements MenuService
{

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<TreeEntity> ztreeAllMenus()
    {
        List<TreeEntity> result = new LinkedList<TreeEntity>();
        List<Map<String, Object>> menus = menuDao.queryAllMenus();
        List<SysMenu> meunList = new LinkedList<SysMenu>();
        for (Map<String, Object> jdbcMapResult : menus)
        {
            // 利用工具类反向生成bean
            meunList.add(reflect(SysMenu.class, jdbcMapResult));
        }
        TreeEntity ztree = null;
        for (SysMenu menu : meunList)
        {
            ztree = new TreeEntity();
            ztree.setId(menu.getId());
            ztree.setName(menu.getText());
            ztree.setParentId(menu.getParentId());
            if (StringUtils.equals("-1", menu.getParentId()))
            {
                ztree.setOpen(true);
            }
            result.add(ztree);
        }
        return result;
    }

    @Override
    public List<TreeEntity> ztreeEditMenus(String roleId)
    {
        List<TreeEntity> result = new LinkedList<TreeEntity>();
        List<Map<String, Object>> menus = menuDao.queryAllMenus();
        List<String> menuIdsOfRole = roleDao.queryMenuIdsByRoleId(roleId);
        List<SysMenu> meunList = new LinkedList<SysMenu>();
        for (Map<String, Object> jdbcMapResult : menus)
        {
            // 利用工具类反向生成bean
            meunList.add(reflect(SysMenu.class, jdbcMapResult));
        }
        TreeEntity ztree = null;
        for (SysMenu menu : meunList)
        {
            ztree = new TreeEntity();
            ztree.setId(menu.getId());
            ztree.setName(menu.getText());
            ztree.setParentId(menu.getParentId());
            if (StringUtils.equals("-1", menu.getParentId()))
            {
                ztree.setOpen(true);
            }
            //判断当前menu是否在该角色所绑定菜单id集合内，如果是，则勾选复选框
            if (menuIdsOfRole.contains(menu.getId()))
            {
                ztree.setChecked(true);
            }
            result.add(ztree);
        }
        return result;
    }

}
